package com.hnkywl.sts.action.xsgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.xsgl.NbxsdDao;
import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.sys.Bm;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.entity.xsgl.Xsd;
import com.hnkywl.sts.entity.xsgl.Xsmx;
import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.xsgl.NbxsdService;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;


 /**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-15
 * Time: 下午2:17
 * To change this template use File | Settings | File Templates.
 */
 @Namespace("/xsgl/nbxsd")
public class NbxsdAction extends SimpleActionSupport {
    private NbxsdService nbxsdService;
    private GsjgService gsjgService;
    private NbxsdDao nbxsdDao;

    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows;
    //销售单ID
    private Long xsdId;
    //销售单
    private Xsd xsd;
    //当前登录员工的公司ID
    private Long gsId;
    private Long zggsId;
    private Yg yg;

    private Date startDate;

    private Date datetime;

    private String ewmtime;
    //查询条件
    private Long xshtId;

    private String zt;

    private String xsdh;

    private Long khId;

    private Long khbmId;

    private Long xsdwId;

    private Long xsgsId;

    private Long xsbmId;

    private Long xsrId;

    private Long gcId;

    private Date fhsjStart;

    private Date fhsjEnd;

    private Long xsjsdId;

    private String sftzd;

    private Bm xsdhm;
    private String ewmType;
    private Date currentTime = new Date();
    private String zdr;
    //配送单号
    private String psdh;
    //库存零售客户
    private Qyjg qyjg;
    //库存零售客户Id
    private Long qyjgId;

    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;
    private String xslx;

    /**
     * 方法名:     销售单查询
     * 功能描述:      销售单查询
     * 输入参数:    <@param cglx 采购合同类型 采购入库，直达到货>
     * 异常:      <RuntimeException  Exception>
     * 创建人:     lingyun
     * 创建日期:   13-3-7上午11:47
     * 修改人:     lingyun
     * 修改日期:   13-3-7上午11:47
     */
    @Action("xsdList")
    public void xsdList() throws ParseException {
        if (xsdwId != null) {
            Gsjg g = this.gsjgService.findGsjgDetail(xsdwId);
            if (g instanceof Nbgs) {
                xsgsId = xsdwId;
            } else {
                xsbmId = xsdwId;
            }
            this.renderJson(this.nbxsdService.findAllXsd(xsdh, fhsjStart, fhsjEnd, xshtId, khId, khbmId,gcId, xsgsId, xsbmId, xsrId, zt, sftzd, page, rows));
        } else {
            this.renderJson(this.nbxsdService.findAllXsd(xsdh, fhsjStart, fhsjEnd, xshtId, khId, khbmId,gcId, xsgsId, xsbmId, xsrId, zt, sftzd, page, rows));
        }
    }

     //功能操作
    @Action("nbxsd")
    public String nbxsd() {
        fhsjStart = sDaysAfterNowDate(1);
        fhsjEnd = sDaysAfterNowDate(0);
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //   当前日期加减n天后的日期，返回String   (yyyy-mm-dd)
    private Date sDaysAfterNowDate(int n) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.add(Calendar.DAY_OF_MONTH, -n);
        return rightNow.getTime();
    }

    @Action("xsdSearchList")
    public void xsdSearchList() {
        this.renderJson(this.nbxsdService.findXsdSearch(xsdh, fhsjStart, fhsjEnd, xsjsdId, xsgsId, khId, 0, 10));
    }

    @Action("xsdXspsList")
    public void xsdXspsList() {
        this.renderJson(this.nbxsdService.findAllXsdNotFyzr(psdh, page, rows));
    }

    @Action("nbxsd_xsmx")
    public void xsmxDetail() {
        this.renderJson(this.nbxsdService.findXsmxDetail(xsdId));
    }

    @Action("nbxsd_js_xsmx")
    public void xsmxJsDetail() {
        this.renderJson(this.nbxsdService.findXsmxJsDetail(xsdId));
    }

    @Action("nbxsd_xsfy")
    public void xsfyDetail() {
        this.renderJson(this.nbxsdService.findXsfyDetail(xsdId));
    }

    @Actions({@Action("nbxsd_content"), @Action("nbxsd_detail")})
    public String xshtContent() {
        try {
            currentTime = new Date();
            zdr = YgService.getCurrentYgName();
            yg = YgService.getCurrentYg();
            gsId = YgService.getCurrentYgGsId();
            zggsId = YgService.getCurrentYgZgGsId();
            if (xsdId != null) {
                xsd = this.nbxsdService.findXsdDetail(xsdId);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
                ewmtime = simpleDateFormat.format(xsd.getLastUpdated());
            } else {
                startDate = new Date(); //当前日期
                Calendar calendar = Calendar.getInstance();//日历对象
                calendar.setTime(startDate);//设置当前日期
                calendar.add(Calendar.MONTH, 1);//将当前日期加一个月
                datetime = calendar.getTime();//输出格式化的日期
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }

    //保存销售单
    @Action("add_xsd")
    public void addXsd() {
        try {
            initXsd(xsd);
            this.nbxsdService.saveXsd(xsd);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存销售单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //修改销售单
    @Action("edit_xsd")
    public void editXsd() {
        try {
            Xsd clone = this.nbxsdService.findXsdDetail(xsd.getId());
            String xsdbm = clone.getXsdh();
            ReflectionUtils.getInstance(clone, xsd);
            initXsd(clone);
            this.nbxsdService.editXsd(clone, xsdbm);
            renderText("{\"statusCode\":\"200\", \"message\":\"保存销售单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //销售结算单中查询销售单
    @Action("xsdmxSearchList")
    public void xgdmxSearchList() {
        this.renderJson(this.nbxsdService.findXsdmxSearch(xsjsdId));
    }

    private void initXsd(Xsd xsd) {
        if (xsd.getXsht() != null && xsd.getXsht().getId() == null) {
            xsd.setXsht(null);
        }
        if (xsd.getLj() != null && xsd.getLj().getId() == null) {
            xsd.setLj(null);
        }
        if (xsd.getKh() != null && xsd.getKh().getId() == null) {
            xsd.setKh(null);
        }
        if (xsd.getThck() != null && xsd.getThck().getId() == null) {
            xsd.setThck(null);
        }
        if (xsd.getGc() != null && xsd.getGc().getId() == null) {
            xsd.setGc(null);
        }
        if (xsd.getFyzr() != null && xsd.getFyzr().getId() == null) {
            xsd.setFyzr(null);
        }
        if (xsd.getXsgs() != null && xsd.getXsgs().getId() == null) {
            xsd.setXsgs(null);
        }
        if (xsd.getXsbm() != null && xsd.getXsbm().getId() == null) {
            xsd.setXsbm(null);
        }
        if (xsd.getXsr() != null && xsd.getXsr().getId() == null) {
            xsd.setXsr(null);
        }
        if (xsd.getXbgs() != null && xsd.getXbgs().getId() == null) {
            xsd.setXbgs(null);
        }
        if (xsd.getXbbm() != null && xsd.getXbbm().getId() == null) {
            xsd.setXbbm(null);
        }
        if (xsd.getXsmxList() != null) {
            for (Xsmx xsmx : xsd.getXsmxList()) {
                if (xsmx.getJzfs() != null && xsmx.getJzfs().getId() == null) {
                    xsmx.setJzfs(null);
                }
            }
        }
    }

    //提交销售单
    @Action("commit_xsd")
    public void commit_xsd() {
        try {
            Xsd xsd = this.nbxsdService.findXsdDetail(xsdId);
//            this.nbxsdService.calcKhSL(xsd, "minus");
            this.nbxsdService.commitXsd(xsd);
            renderText("{\"statusCode\":\"200\", \"message\":\"提交销售单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("cancel_xsd")
    public void cancel_xsd() {
        try {
            Xsd xsd = this.nbxsdService.findXsdDetail(xsdId);
            this.nbxsdService.cancel(xsd);
//            this.nbxsdService.calcKhSL(xsd, "add");
            renderText("{\"statusCode\":\"200\", \"message\":\"提交销售单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //删除销售单
    @Action("delete_xsd")
    public void delete_xsd() {
        try {
            Xsd xsd = this.nbxsdService.deleteXsd(xsdId);
            renderText("{\"statusCode\":\"200\", \"message\":\"废除销售单成功\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("xsdKhList")
    public void xsdKhList() {
        this.renderJson(this.nbxsdService.findKhByXsd(xsgsId));
    }

    @Action("xsdDetail")
    public void xsdDetail() {
        Gson gson = new Gson();
        List<Map> xsdmxList = this.nbxsdDao.findXsmxList(xsdId);
        Double xscb=0d;
        Integer js = 0;
        Double sbsl = 0d;
        Double ck_sbsl = 0d;
        Double hsj = 0d;
        Double bhsj = 0d;
        Double je = 0d;
        Double se = 0d;
        String result = "";
        java.text.DecimalFormat df=new java.text.DecimalFormat("0.0000");
        java.text.DecimalFormat jtdf=new java.text.DecimalFormat("0.00");
        StringBuffer sbTable = new StringBuffer();
        if (null != xsdmxList) {
            sbTable.append("<table class=\"tb intb\">");
            sbTable.append("<tr>");
            sbTable.append("<td>物资编码</td><td>品名</td><td>规格型号</td><td>记重方式</td><td>卡号</td><td>销售成本(元)</td><td>件数</td>" +
                    "  <td>应发数量</td><td name='cksl_name' >出库数量</td><td name='cksl_name' >不含税单价(元)</td>" +
                    "  <td name='cksl_name' >不含税总金额(元)</td><td name='cksl_name' >含税单价(元)</td><td name='cksl_name' >含税总金额(元)</td>" /*+*/
                    /*"<td>备注</td>"*/);
            sbTable.append("</tr>");

            for (int i = 0; i < xsdmxList.size(); i++) {
                sbTable.append("<tr>");
                // 物资编码
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.code")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.code"));
                }
                sbTable.append("</td>");
                //    品名
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.name")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.name"));
                }
                sbTable.append("</td>");
                //  规格型号
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("wzgg.ggxh")) {
                    sbTable.append(xsdmxList.get(i).get("wzgg.ggxh"));
                }
                sbTable.append("</td>");
                //记重方式
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("jzfs.z")) {
                    sbTable.append(xsdmxList.get(i).get("jzfs.z"));
                }
                sbTable.append("</td>");
                //卡号
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("kh.khbm")) {
                    sbTable.append(xsdmxList.get(i).get("kh.khbm"));
                }
                sbTable.append("</td>");

                //销售成本
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("xscb")) {
                    sbTable.append(df.format(xsdmxList.get(i).get("xscb")));
                    xscb += (Double) (xsdmxList.get(i).get("xscb"));
                }
                sbTable.append("</td>");
                //件数
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("js")) {
                    sbTable.append(xsdmxList.get(i).get("js"));
                    js += (Integer) (xsdmxList.get(i).get("js"));
                }
                sbTable.append("</td>");
                // 应发数量
                sbTable.append("<td>");
                if (null != xsdmxList.get(i).get("sbsl")) {
                    sbTable.append(jtdf.format(xsdmxList.get(i).get("sbsl")));
                    sbsl += (Double) (xsdmxList.get(i).get("sbsl"));
                }
                sbTable.append("</td>");
                //出库数量
                sbTable.append("<td name='cksl_name'> ");
                if (null != xsdmxList.get(i).get("ck_sbsl")) {
                    sbTable.append(df.format(xsdmxList.get(i).get("ck_sbsl")));
                    ck_sbsl += (Double) (xsdmxList.get(i).get("ck_sbsl"));
                }
                sbTable.append("</td>");
                //   不含税价
                sbTable.append("<td name='cksl_name'>");
                if (null != xsdmxList.get(i).get("bhsj")) {
                    sbTable.append(df .format(xsdmxList.get(i).get("bhsj")));
                }
                sbTable.append("</td>");
                //不含税额
                sbTable.append("<td name='cksl_name'> ");
                if (null != xsdmxList.get(i).get("bhsj")&&null != xsdmxList.get(i).get("sbsl")) {
                    Double bhse=((Double)(xsdmxList.get(i).get("bhsj")))*((Double)(xsdmxList.get(i).get("sbsl")));
                    sbTable.append(jtdf.format(bhse));
                    je += bhse;
                }
                sbTable.append("</td>");

                //含税价
                sbTable.append("<td name='cksl_name'> ");
                if (null != xsdmxList.get(i).get("hsj")) {
                    sbTable.append(df.format(xsdmxList.get(i).get("hsj")));
                }
                sbTable.append("</td>");

                // 含税额
                sbTable.append("<td name='cksl_name'> ");
                if (null != xsdmxList.get(i).get("hsj")&&null != xsdmxList.get(i).get("sbsl")) {
                    Double hse=((Double)(xsdmxList.get(i).get("hsj")))*((Double)(xsdmxList.get(i).get("sbsl")));
                    sbTable.append(jtdf.format(hse));
                    se += hse;
                }
                sbTable.append("</td>");
                sbTable.append("</tr>");
            }
            sbTable.append("<tr>");

            sbTable.append("<th colspan=\"5\">合计:");
            sbTable.append("</th>");

            sbTable.append("<td>");
            sbTable.append(df.format(xscb));
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(js);
            sbTable.append("</td>");

            sbTable.append("<td>");
            sbTable.append(df.format(sbsl));
            sbTable.append("</td>");

            sbTable.append("<td name='cksl_name'>");
            sbTable.append("</td>");

            sbTable.append("<td name='cksl_name'>");
            sbTable.append("</td>");

            sbTable.append("<td name='cksl_name'>");
            sbTable.append(jtdf.format(je));
            sbTable.append("</td>");

            sbTable.append("<td name='cksl_name'>");
            sbTable.append("</td>");

            sbTable.append("<td name='cksl_name'>");
            sbTable.append(jtdf.format(se));
            sbTable.append("</td>");


            sbTable.append("</tr>");
            sbTable.append("</table>");
        }
        renderHtml(sbTable.toString());
    }

    @Action("xsdEwm")
    public void xsdEwm(){
        List<Map> xsdmxList = this.nbxsdDao.findXsmxList(xsdId);
        StringBuffer sbTable = new StringBuffer();
    }

    public String getXslx() {
        return xslx;
    }

    public void setXslx(String xslx) {
        this.xslx = xslx;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getXsdId() {
        return xsdId;
    }

    public void setXsdId(Long xsdId) {
        this.xsdId = xsdId;
    }

    public Date getFhsjStart() {
        return fhsjStart;
    }

    public void setFhsjStart(Date fhsjStart) {
        this.fhsjStart = fhsjStart;
    }

    public Date getFhsjEnd() {
        return fhsjEnd;
    }

    public void setFhsjEnd(Date fhsjEnd) {
        this.fhsjEnd = fhsjEnd;
    }

    public Xsd getXsd() {
        return xsd;
    }

    public void setXsd(Xsd xsd) {
        this.xsd = xsd;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public Long getXshtId() {
        return xshtId;
    }

    public void setXshtId(Long xshtId) {
        this.xshtId = xshtId;
    }

    public String getXsdh() {
        return xsdh;
    }

    public void setXsdh(String xsdh) {
        this.xsdh = xsdh;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public String getEwmtime() {
        return ewmtime;
    }

    public void setEwmtime(String ewmtime) {
        this.ewmtime = ewmtime;
    }

     public Long getKhbmId() {
         return khbmId;
     }

     public void setKhbmId(Long khbmId) {
         this.khbmId = khbmId;
     }

     public Long getXsgsId() {
        return xsgsId;
    }

    public void setXsgsId(Long xsgsId) {
        this.xsgsId = xsgsId;
    }

    public Long getXsbmId() {
        return xsbmId;
    }

    public void setXsbmId(Long xsbmId) {
        this.xsbmId = xsbmId;
    }

    public Long getXsrId() {
        return xsrId;
    }

    public void setXsrId(Long xsrId) {
        this.xsrId = xsrId;
    }

    public Long getGcId() {
        return gcId;
    }

    public void setGcId(Long gcId) {
        this.gcId = gcId;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Long getZggsId() {
        return zggsId;
    }

    public void setZggsId(Long zggsId) {
        this.zggsId = zggsId;
    }

    public Long getXsjsdId() {
        return xsjsdId;
    }

    public void setXsjsdId(Long xsjsdId) {
        this.xsjsdId = xsjsdId;
    }

    public Bm getXsdhm() {
        return xsdhm;
    }

    public void setXsdhm(Bm xsdhm) {
        this.xsdhm = xsdhm;
    }

    public String getSftzd() {
        return sftzd;
    }

    public void setSftzd(String sftzd) {
        this.sftzd = sftzd;
    }

    public Qyjg getQyjg() {
        return qyjg;
    }

    public void setQyjg(Qyjg qyjg) {
        this.qyjg = qyjg;
    }

    public Long getQyjgId() {
        return qyjgId;
    }

    public void setQyjgId(Long qyjgId) {
        this.qyjgId = qyjgId;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Date getCurrentTime() {
        return currentTime;
    }

    public String getPsdh() {
        return psdh;
    }

    public void setPsdh(String psdh) {
        this.psdh = psdh;
    }

    public void setCurrentTime(Date currentTime) {
        this.currentTime = currentTime;
    }

    public String getEwmType() {
        return ewmType;
    }

    public void setEwmType(String ewmType) {
        this.ewmType = ewmType;
    }

    public String getZdr() {
        return zdr;
    }

    public void setZdr(String zdr) {
        this.zdr = zdr;
    }

    public Long getXsdwId() {
        return xsdwId;
    }

    public void setXsdwId(Long xsdwId) {
        this.xsdwId = xsdwId;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }

    @Autowired
     public void setNbxsdService(NbxsdService nbxsdService) {
         this.nbxsdService = nbxsdService;
     }
     @Autowired
     public void setNbxsdDao(NbxsdDao nbxsdDao) {
         this.nbxsdDao = nbxsdDao;
     }
 }
